<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-future.html" />
<link rel="prev" href="module-atexit.html" />
<link rel="parent" href="python.html" />
<link rel="next" href="traceback-example.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>26.7 traceback -- Print or retrieve a stack traceback</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="26.6.1 atexit Example"
  href="atexit-example.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="26. python Runtime Services"
  href="python.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="26.7.1 traceback Example"
  href="traceback-example.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="atexit-example.html">26.6.1 atexit Example</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="python.html">26. Python Runtime Services</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="traceback-example.html">26.7.1 Traceback Example</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0028700000000000000000">
26.7 <tt class="module">traceback</tt> --
         Print or retrieve a stack traceback</a>
</h1>

<p>
<a name="module-traceback"></a>

<p>
This module provides a standard interface to extract, format and print
stack traces of Python programs.  It exactly mimics the behavior of
the Python interpreter when it prints a stack trace.  This is useful
when you want to print stack traces under program control, such as in a
``wrapper'' around the interpreter.

<p>
The module uses traceback objects -- this is the object type that is
stored in the variables <code>sys.exc_traceback</code> (deprecated) and
<code>sys.last_traceback</code> and returned as the third item from
<tt class="function">sys.exc_info()</tt>.
<a id='l2h-5220' xml:id='l2h-5220'></a>
<p>
The module defines the following functions:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5221' xml:id='l2h-5221' class="function">print_tb</tt></b>(</nobr></td>
  <td><var>traceback</var><big>[</big><var>, limit</var><big>[</big><var>, file</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Print up to <var>limit</var> stack trace entries from <var>traceback</var>.  If
<var>limit</var> is omitted or <code>None</code>, all entries are printed.
If <var>file</var> is omitted or <code>None</code>, the output goes to
<code>sys.stderr</code>; otherwise it should be an open file or file-like
object to receive the output.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5222' xml:id='l2h-5222' class="function">print_exception</tt></b>(</nobr></td>
  <td><var>type, value, traceback</var><big>[</big><var>,
                                  limit</var><big>[</big><var>, file</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Print exception information and up to <var>limit</var> stack trace entries
from <var>traceback</var> to <var>file</var>.
This differs from <tt class="function">print_tb()</tt> in the
following ways: (1) if <var>traceback</var> is not <code>None</code>, it prints a
header "<tt class="samp">Traceback (most recent call last):</tt>"; (2) it prints the
exception <var>type</var> and <var>value</var> after the stack trace; (3) if
<var>type</var> is <tt class="exception">SyntaxError</tt> and <var>value</var> has the
appropriate format, it prints the line where the syntax error occurred
with a caret indicating the approximate position of the error.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5223' xml:id='l2h-5223' class="function">print_exc</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>limit</var><big>[</big><var>, file</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
This is a shorthand for <code>print_exception(sys.exc_type,
sys.exc_value, sys.exc_traceback, <var>limit</var>, <var>file</var>)</code>.  (In
fact, it uses <tt class="function">sys.exc_info()</tt> to retrieve the same
information in a thread-safe way instead of using the deprecated
variables.)
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5224' xml:id='l2h-5224' class="function">format_exc</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>limit</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
This is like <code>print_exc(<var>limit</var>)</code> but returns a string
instead of printing to a file.

<span class="versionnote">New in version 2.4.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5225' xml:id='l2h-5225' class="function">print_last</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>limit</var><big>[</big><var>, file</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
This is a shorthand for <code>print_exception(sys.last_type,
sys.last_value, sys.last_traceback, <var>limit</var>, <var>file</var>)</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5226' xml:id='l2h-5226' class="function">print_stack</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>f</var><big>[</big><var>, limit</var><big>[</big><var>, file</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
This function prints a stack trace from its invocation point.  The
optional <var>f</var> argument can be used to specify an alternate stack
frame to start.  The optional <var>limit</var> and <var>file</var> arguments have the
same meaning as for <tt class="function">print_exception()</tt>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5227' xml:id='l2h-5227' class="function">extract_tb</tt></b>(</nobr></td>
  <td><var>traceback</var><big>[</big><var>, limit</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a list of up to <var>limit</var> ``pre-processed'' stack trace
entries extracted from the traceback object <var>traceback</var>.  It is
useful for alternate formatting of stack traces.  If <var>limit</var> is
omitted or <code>None</code>, all entries are extracted.  A
``pre-processed'' stack trace entry is a quadruple (<var>filename</var>,
<var>line number</var>, <var>function name</var>, <var>text</var>) representing
the information that is usually printed for a stack trace.  The
<var>text</var> is a string with leading and trailing whitespace
stripped; if the source is not available it is <code>None</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5228' xml:id='l2h-5228' class="function">extract_stack</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>f</var><big>[</big><var>, limit</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Extract the raw traceback from the current stack frame.  The return
value has the same format as for <tt class="function">extract_tb()</tt>.  The
optional <var>f</var> and <var>limit</var> arguments have the same meaning as
for <tt class="function">print_stack()</tt>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5229' xml:id='l2h-5229' class="function">format_list</tt></b>(</nobr></td>
  <td><var>list</var>)</td></tr></table></dt>
<dd>
Given a list of tuples as returned by <tt class="function">extract_tb()</tt> or
<tt class="function">extract_stack()</tt>, return a list of strings ready for
printing.  Each string in the resulting list corresponds to the item
with the same index in the argument list.  Each string ends in a
newline; the strings may contain internal newlines as well, for those
items whose source text line is not <code>None</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5230' xml:id='l2h-5230' class="function">format_exception_only</tt></b>(</nobr></td>
  <td><var>type, value</var>)</td></tr></table></dt>
<dd>
Format the exception part of a traceback.  The arguments are the
exception type and value such as given by <code>sys.last_type</code> and
<code>sys.last_value</code>.  The return value is a list of strings, each
ending in a newline.  Normally, the list contains a single string;
however, for <tt class="exception">SyntaxError</tt> exceptions, it contains several
lines that (when printed) display detailed information about where the
syntax error occurred.  The message indicating which exception
occurred is the always last string in the list.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5231' xml:id='l2h-5231' class="function">format_exception</tt></b>(</nobr></td>
  <td><var>type, value, tb</var><big>[</big><var>, limit</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Format a stack trace and the exception information.  The arguments 
have the same meaning as the corresponding arguments to
<tt class="function">print_exception()</tt>.  The return value is a list of strings,
each ending in a newline and some containing internal newlines.  When
these lines are concatenated and printed, exactly the same text is
printed as does <tt class="function">print_exception()</tt>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5232' xml:id='l2h-5232' class="function">format_tb</tt></b>(</nobr></td>
  <td><var>tb</var><big>[</big><var>, limit</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
A shorthand for <code>format_list(extract_tb(<var>tb</var>, <var>limit</var>))</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5233' xml:id='l2h-5233' class="function">format_stack</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>f</var><big>[</big><var>, limit</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
A shorthand for <code>format_list(extract_stack(<var>f</var>, <var>limit</var>))</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5234' xml:id='l2h-5234' class="function">tb_lineno</tt></b>(</nobr></td>
  <td><var>tb</var>)</td></tr></table></dt>
<dd>
This function returns the current line number set in the traceback
object.  This function was necessary because in versions of Python
prior to 2.3 when the <b class="programopt">-O</b> flag was passed to Python the
<code><var>tb</var>.tb_lineno</code> was not updated correctly.  This function
has no use in versions past 2.3.
</dl>

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="traceback-example.html">26.7.1 Traceback Example</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="26.6.1 atexit Example"
  href="atexit-example.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="26. python Runtime Services"
  href="python.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="26.7.1 traceback Example"
  href="traceback-example.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="atexit-example.html">26.6.1 atexit Example</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="python.html">26. Python Runtime Services</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="traceback-example.html">26.7.1 Traceback Example</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
